<?php
/**
 * (ajax) 采购单
 */
namespace App\Admin\Ajax\Purchase;

use App\Models\Inventory\AreaCn;
use App\Models\Purchase\PurchaseOrderDetail;
use App\Models\Sys\Logs;
use App\Models\Basics\Supplier;
use App\Models\Basics\SupplierContact;
use App\Models\Basics\SysArea;
use App\Models\Inventory\InventoryCn;
use App\Models\Purchase\PurchaseOrder;
use App\Models\Purchase\PurchaseOrderContract;
use App\Models\Stocking\StockingApply;
use App\Models\Stocking\StockingApplyDetail;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
class OrderAjax {

    /**
     * 撤销计划
     * （删除采购单关联单据，并更新备货单和备货明细）
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function planRevoke($id){
        try{
            DB::beginTransaction();
            $model = PurchaseOrder::query()->findOrFail($id);
            if($model->status != PurchaseOrder::STATUS_NO){ //未下单
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '状态有误！']);
            }
            $flag1 = false;
            foreach ($model->order_detail as $detail){
                $flag2 = false;
                foreach ($detail->stocking_detail as $item){
                    //更新备货单计划状态
                    $flag3 = $this->_upPlanStatus($item->apply_detail_id,$item->plan_num);
                    if(!$flag3){
                        $flag2 = false;
                        break;
                    }
                    $flag2 = true;
                }
                if(!$flag2){
                    $flag1 = false;
                    break;
                }
                $flag1 = true;
            }
            if(!$flag1){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }
            $model->delete();
            DB::commit();
            return response()->json(['status' => true, 'message' => '操作成功！']);
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * * 撤销计划 *
     * 更新备货单计划状态
     * @param $applyDetailId
     * @param $planNum
     * @return bool
     */
    private function _upPlanStatus($applyDetailId,$planNum){
        DB::beginTransaction();

        //更新备货单明细
        $sad = StockingApplyDetail::query()->find($applyDetailId);
        $sad->plan_num -= $planNum; //计划采购数量
        if($sad->plan_num >= $sad->apply_detail_num){
            $sad->plan_status = StockingApplyDetail::PLAN_STATUS_ALL; //全部计划
        }elseif($sad->plan_num > 0 && $sad->plan_num < $sad->apply_detail_num){
            $sad->plan_status = StockingApplyDetail::PLAN_STATUS_PART; //部分计划
        }else{
            $sad->plan_status = StockingApplyDetail::PLAN_STATUS_NO; //未计划
        }
        $flag1 = $sad->save();
        if(!$flag1){
            DB::rollBack();
            return false;
        }
        $detailPlanStatus = StockingApplyDetail::query()->where('stocking_no',$sad->stocking_no)->pluck('plan_status')->toArray();
        $planStatus = StockingApplyDetail::extractStatus($detailPlanStatus);

        //更新备货单
        $sa = StockingApply::query()->where('stocking_no',$sad->stocking_no)->first();
        $sa->plan_status = $planStatus;
        $flag2 = $sa->save();
        if(!$flag2){
            DB::rollBack();
            return false;
        }
        DB::commit();
        return true;
    }

    /**
     * 撤销下单
     * 1.回写备货明细的交货日期和下单状态
     * 2.更新PUBLIC在途数量
     * 3.更新站点在途数量
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function purRevoke($id){
        try{
            DB::beginTransaction();
            $model = PurchaseOrder::query()->findOrFail($id);
            if($model->status != PurchaseOrder::STATUS_YES){ //已下单
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '状态有误！']);
            }
            if($model->received_status != PurchaseOrder::RECEIVED_NO){ //未收货
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '已生成下游单据！']);
            }

            //更新采购单
            $model->status = PurchaseOrder::STATUS_NO; //未下单
            $model->order_at = null;
            $flag1 = $model->save();
            if(!$flag1){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }

            $areaPublicId = $this->_getSysAreaPublicId(); //公共站点id
            $flag2 = false;
            foreach ($model->order_detail as $detail){
                //扣减PUBLIC站点在途
                $flag3 = $this->_purRevokeRewritePublic($areaPublicId,$detail->psku_id,$detail->order_num);
                if(!$flag3){
                    $flag2 = false;
                    break;
                }
                $flag4 = false;

                //采购备货明细
                foreach ($detail->stocking_detail as $item){
                    $itemOrderNum = $item->order_num;
                    $item->order_num = 0;
                    $flag5 = $item->save();
                    if(!$flag5){
                        $flag4 = false;
                        break;
                    }
                    //回写备货单，站点在途
                    $flag6 = $this->_purRevokeRewriteStocking($areaPublicId,$item->apply_detail_id,$itemOrderNum);
                    if(!$flag6){
                        $flag4 = false;
                        break;
                    }
                    $flag4 = true;
                }
                if(!$flag4){
                    $flag2 = false;
                    break;
                }
                $flag2 = true;
            }
            if($flag2){
                Logs::logs('purchase',"采购单：{$model->purchase_no}撤销下单",$id);
                DB::commit();
                return response()->json(['status' => true, 'message' => '操作成功！']);
            }else{
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * *撤销下单*
     * 回写PUBLIC在途
     * @param $areaId
     * @param $pSkuId
     * @param $orderNum
     * @return bool
     */
    private function _purRevokeRewritePublic($areaId,$pSkuId,$orderNum){
        DB::beginTransaction();
        $inventory = InventoryCn::query()
            ->where('area_id', $areaId)
            ->where('psku_id', $pSkuId)
            ->lockForUpdate()
            ->first();
        if(!$inventory){
            DB::rollBack();
            return false;
        }
        $inventory->transit -= min($inventory->transit,$orderNum);
        $inventory->save();
        DB::commit();
        return true;
    }

    /**
     * *撤销下单*
     * 回写备货单，站点在途
     * @param $areaPublicId
     * @param $applyDetailId
     * @param $orderNum
     * @return bool
     */
    private function _purRevokeRewriteStocking($areaPublicId,$applyDetailId,$orderNum){
        DB::beginTransaction();

        //更新备货单明细
        $sad = StockingApplyDetail::query()->find($applyDetailId);
        $sad->pur_status = StockingApplyDetail::PURCHASE_STATUS_NO; //未下单
        $sad->commit_date = null; //交期
        $flag1 = $sad->save();
        if(!$flag1){
            DB::rollBack();
            return false;
        }
        $detailPurStatus = StockingApplyDetail::query()->where('stocking_no',$sad->stocking_no)->pluck('pur_status')->toArray();
        $purStatus = StockingApplyDetail::extractStatus($detailPurStatus);

        //更新备货单
        $sa = StockingApply::query()->where('stocking_no',$sad->stocking_no)->first();
        $sa->pur_status = $purStatus;
        $flag2 = $sa->save();
        if(!$flag2){
            DB::rollBack();
            return false;
        }

        //更新非PUBLIC站点的国内在途库存(更新站点库存配额)
        if($areaPublicId != $sad->area_id){ //不是PUBLIC即更新站点在途
            $mPSkuId = $sa->sku->m_psku_id ?? 0;
            if(!$mPSkuId){
                DB::rollBack();
                return false;
            }
            if($mPSkuId == $sad->psku_id){ //主产品
                $areaTransit = (int)($orderNum / $sa->sku->m_num);
                $inventory = AreaCn::query()
                    ->where('area_id' , $sa->area_id)
                    ->where('psku_id' , $sa->psku_id)
                    ->lockForUpdate()
                    ->first();
                if(!$inventory){
                    DB::rollBack();
                    return false;
                }
                $inventory->area_transit -= min($inventory->area_transit,$areaTransit);
                $flag3 = $inventory->save();
                if(!$flag3){
                    DB::rollBack();
                    return false;
                }
            }
        }
        DB::commit();
        return true;
    }

    /**
     * 下单
     * 1.回写备货明细的交货日期和下单状态
     * 2.更新PUBLIC在途数量
     * 3.更新站点在途数量
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function purOrder($id){
        try{
            DB::beginTransaction();

            //更新采购单
            $model = PurchaseOrder::query()->findOrFail($id);
            if($model->status != PurchaseOrder::STATUS_NO){ //未下单
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '状态有误！']);
            }
            if(!$model->commit_date){ //交期
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '交期为空！']);
            }
            $model->status = PurchaseOrder::STATUS_YES; //已下单
            $model->order_at = date('Y-m-d H:i:s'); //订单时间
            $flag1 = $model->save();
            if(!$flag1){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }

            $areaPublicId = $this->_getSysAreaPublicId(); //PUBLIC公共站点
            $flag2 = false;
            foreach ($model->order_detail as $detail){
                //添加PUBLIC站点在途数量
                $flag3 = $this->_purOrderRewritePublic($areaPublicId,$detail->psku_id,$detail->order_num);
                if(!$flag3){
                    $flag2 = false;
                    break;
                }
                $count = $detail->stocking_detail->count();
                $surplus = $i = 0;
                $flag4 = false;
                foreach ($detail->stocking_detail as $item){
                    //更新采购备货，订单数量
                    $i++;
                    if($i == $count){
                        $_thisOrderNum = $detail->order_num - $surplus;
                    }else{
                        $_thisOrderNum = (int)floor($item->plan_num / $detail->plan_num * $detail->order_num);
                        $surplus += $_thisOrderNum;
                    }
                    $item->order_num = $_thisOrderNum;
                    $flag5 = $item->save();
                    if(!$flag5){
                        $flag4 = false;
                        break;
                    }

                    //更新备货单据，以及国内站点在途(更新站点库存配额)
                    $flag6 = $this->_purOrderRewriteStocking($areaPublicId,$item->apply_detail_id,$model->commit_date,$item->order_num);
                    if(!$flag6){
                        $flag4 = false;
                        break;
                    }
                    $flag4 = true;
                }
                if(!$flag4){
                    $flag2 = false;
                    break;
                }
                $flag2 = true;
            }
            if($flag2){
                Logs::logs('purchase',"采购单：{$model->purchase_no}下单",$id);
                DB::commit();
                return response()->json(['status' => true, 'message' => '操作成功！']);
            }else{
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * *下单*
     * 回写PUBLIC在途
     * @param $areaId
     * @param $pSkuId
     * @param $orderNum
     * @return bool
     */
    private function _purOrderRewritePublic($areaId,$pSkuId,$orderNum){
        DB::beginTransaction();
        $inventory = InventoryCn::query()->firstOrNew(['area_id' => $areaId,'psku_id' => $pSkuId]);
        $inventory->transit += $orderNum;
        $inventory->save();
        DB::commit();
        return true;
    }

    /**
     * *下单*
     * 回写备货单，站点在途
     * @param $areaPublicId
     * @param $applyDetailId
     * @param $commitDate
     * @param $orderNum
     * @return bool
     */
    private function _purOrderRewriteStocking($areaPublicId,$applyDetailId,$commitDate,$orderNum){
        DB::beginTransaction();
        //备货单明细
        $sad = StockingApplyDetail::query()->find($applyDetailId);
        $sad->pur_status = StockingApplyDetail::PURCHASE_STATUS_YES; //已下单
        $sad->commit_date = $commitDate; //交期
        $flag1 = $sad->save();
        if(!$flag1){
            DB::rollBack();
            return false;
        }

        //备货单
        $detailPurStatus = StockingApplyDetail::query()->where('stocking_no',$sad->stocking_no)->pluck('pur_status')->toArray();
        $purStatus = StockingApplyDetail::extractStatus($detailPurStatus);
        $sa = StockingApply::query()->where('stocking_no',$sad->stocking_no)->first();
        $sa->pur_status = $purStatus;
        $flag2 = $sa->save();
        if(!$flag2){
            DB::rollBack();
            return false;
        }

        //更新国内站点在途(更新站点库存配额)
        if($areaPublicId != $sad->area_id){ //不是PUBLIC即更新站点在途（其他站点）
            $mPSkuId = $sa->sku->m_psku_id ?? 0;
            if(!$mPSkuId){
                DB::rollBack();
                return false;
            }
            if($mPSkuId == $sad->psku_id){ //主产品
                $inventory = AreaCn::query()->firstOrNew(['area_id' => $sa->area_id,'psku_id' => $sa->psku_id]);
                $inventory->area_transit += (int)($orderNum / $sa->sku->m_num);
                $flag3 = $inventory->save();
                if(!$flag3){
                    DB::rollBack();
                    return false;
                }
            }
        }
        DB::commit();
        return true;
    }

    /**
     * 采购合同确认
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function purContract($id)
    {
        $purchase = PurchaseOrder::query()->findOrFail($id);
        $supplier = Supplier::query()->findOrFail($purchase->supplier_id);
        $supplierContact = SupplierContact::query()
            ->select('contact','cellphone')
            ->where('supplier_id',$purchase->supplier_id)
            ->orderBy('id','ASC')
            ->first();
        return view('purchase.contract-confirm.index',compact('id','purchase','supplier','supplierContact'));
    }

    /**
     * 生成采购合同
     * @param $param
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function createPurContract($param)
    {
        $file = PurchaseOrderContract::getFile($param['purchase_no'],$param['file_type']);
        if(is_file($file)){
            PurchaseOrderContract::storeFile($param['purchase_no'],$param['file_type']);
            return response()->json(['status' => true,'message' => '合同已生成!']);
        }
        try{
            Artisan::call('po:contract', ['param' => $param]);
            return response()->json(['status' => true, 'message' => '合同生成完成！']);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * 删除合同文件
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delPurContract($id){
        try{
            DB::beginTransaction();
            $poc = PurchaseOrderContract::query()->findOrFail($id);
            $path = PurchaseOrderContract::getFile($poc->purchase_no,$poc->type);
            $flag = $poc->delete();
            if(!$flag){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }
            if(file_exists($path)){
                unlink($path);
            }
            DB::commit();
            return response()->json(['status' => true, 'message' => '操作成功！']);
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * 下载合同文件
     * @param $id
     * @return mixed
     */
    public function exportPurContract($id){
        $poc = PurchaseOrderContract::query()->findOrFail($id);
        $path = PurchaseOrderContract::getFile($poc->purchase_no,$poc->type);
        return response()->download($path);
    }

    /**
     * 关闭（采购单）
     * 1.回写备货明细的交货日期和下单状态
     * 2.更新PUBLIC在途数量
     * 3.更新站点在途数量
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function purClose($id){
        try{
            DB::beginTransaction();
            $model = PurchaseOrder::query()->findOrFail($id);
            if($model->status != PurchaseOrder::STATUS_YES){ //已下单
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '状态有误！']);
            }
            $model->status = PurchaseOrder::STATUS_CLOSE; //已关闭
            $flag1 = $model->save();
            if(!$flag1){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }

            $areaPublicId = $this->_getSysAreaPublicId(); //PUBLIC公共站点
            $flag2 = false;
            foreach($model->order_detail as $detail){
                if($detail->received_status != PurchaseOrderDetail::RECEIVED_ALL){
                    //（非完全到货的情况下）
                    $noRGoodsNum = $detail->order_num - $detail->rgoods_num;
                    $flag3 = $this->_purCloseRewritePublic($areaPublicId,$detail->psku_id,$noRGoodsNum); //扣减PUBLIC站点在途
                    if(!$flag3){
                        $flag2 = false;
                        break;
                    }
                    $applyDetailIds = $detail->stocking_detail->pluck('apply_detail_id');
                    $flag4 = $this->_purCloseRewriteStocking($areaPublicId,$applyDetailIds,$noRGoodsNum); //更新备货状态，并扣减非PUBLIC站点在途（分配站点在途库存（库存配额））
                    if(!$flag4){
                        $flag2 = false;
                        break;
                    }
                }else{
                    //（完全到货的情况下）
                    $flag5 = false;
                    foreach ($detail->stocking_detail as $item){
                        $flag6 = $this->_purCloseRewrite_Stocking($item->apply_detail_id); //更新备货状态
                        if(!$flag6){
                            $flag5 = false;
                            break;
                        }
                        $flag5 = true;
                    }
                    if(!$flag5){
                        $flag2 = false;
                        break;
                    }
                }
                $flag2 = true;
            }
            if($flag2){
                Logs::logs('purchase',"采购单：{$model->purchase_no}关闭",$id);
                DB::commit();
                return response()->json(['status' => true,'message' => '操作成功！']);
            }else{
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * *关闭*
     * 若有在途需扣减在途
     * @param $areaId
     * @param $pSkuId
     * @param $noRGoodsNum
     * @return bool
     */
    private function _purCloseRewritePublic($areaId,$pSkuId,$noRGoodsNum){
        DB::beginTransaction();
        $inventory = InventoryCn::query()
            ->where('area_id', $areaId)
            ->where('psku_id', $pSkuId)
            ->lockForUpdate()
            ->first();
        if(!$inventory){
            DB::rollBack();
            return false;
        }
        $inventory->transit -= min($inventory->transit,$noRGoodsNum);
        $inventory->save();
        DB::commit();
        return true;
    }

    /**
     * *关闭*
     * 若有在途需扣减站点在途 （分配站点在途库存（库存配额））
     * 回写备货单状态
     * @param $areaPublicId
     * @param $applyDetailIds
     * @param $noRGoodsNum
     * @return bool
     */
    private function _purCloseRewriteStocking($areaPublicId,$applyDetailIds,$noRGoodsNum){
        DB::beginTransaction();
        $sku = '';
        $flag = false;
        foreach ($applyDetailIds as $applyDetailId){
            //备货单明细
            $sad = StockingApplyDetail::query()->find($applyDetailId);
            $sad->pur_status = StockingApplyDetail::PURCHASE_STATUS_CLOSE;  //已关闭
            $flag1 = $sad->save();
            if(!$flag1){
                $flag = false;
                break;
            }

            //备货单
            $detailPurStatus = StockingApplyDetail::query()->where('stocking_no',$sad->stocking_no)->pluck('pur_status')->toArray();
            $purStatus = StockingApplyDetail::extractStatus($detailPurStatus);
            $sa = StockingApply::query()->where('stocking_no',$sad->stocking_no)->first();
            $sa->pur_status = $purStatus;
            $flag2 = $sa->save();
            if(!$flag2){
                $flag = false;
                break;
            }

            //更新非PUBLIC站点在途（更新其他站点在途）
            if($areaPublicId != $sad->area_id){
                $mPSkuId = $sa->sku->m_psku_id ?? 0;
                if(!$mPSkuId){
                    $flag = false;
                    break;
                }
                if($mPSkuId == $sad->psku_id){ //主产品
                    if(!$sku){
                        $sku = $sa->psku_id.'_'.$sa->sku->m_num;
                    }
                }
            }
            $flag = true;
        }
        if(!$flag){
            DB::rollBack();
            return false;
        }
        if(!$sku){
            DB::commit();
            return true;
        }
        //根据站点在途按比例扣减在途数量
        list($pSkuId,$mNum) = explode('_',$sku);
        $inventory = AreaCn::query()
            ->where('area_id' , '<>', $areaPublicId)
            ->where('psku_id' , $pSkuId)
            ->lockForUpdate()
            ->get();
        if($inventory->isEmpty()){
            DB::commit();
            return true;
        }
        $sum = 0;
        foreach ($inventory as $transit){
            $sum += $transit->area_transit * $mNum;
        }
        if($sum == 0){
            DB::commit();
            return true;
        }
        $_noRGoodsNum = min($sum,$noRGoodsNum);
        $count = $inventory->count();
        $surplus = $i = 0;
        $flag3 = false;

        //分配站点在途库存(更新站点库存配额)
        foreach ($inventory as $_inventory){
            $i++;
            $_transit = $_inventory->area_transit;
            if($_transit == 0){
                $flag3 = true;
                continue;
            }
            if($i == $count){
                $_thisNoRGoodsNum = $_noRGoodsNum - $surplus;
            }else{
                $_thisNoRGoodsNum = (int)floor($_transit / $sum * $_noRGoodsNum);
                $surplus += $_thisNoRGoodsNum;
            }
            $areaTransit = (int)($_thisNoRGoodsNum / $mNum);
            $_inventory->area_transit -= min($_transit,$areaTransit); //站点在途库存
            $flag4 = $_inventory->save();
            if(!$flag4){
                $flag3 = false;
                break;
            }
            $flag3 = true;
        }
        if(!$flag3){
            DB::rollBack();
            return false;
        }
        DB::commit();
        return true;
    }

    /**
     * *关闭*
     * 若无在途直接更新状态
     * @param $applyDetailId
     * @return bool
     */
    private function _purCloseRewrite_Stocking($applyDetailId){
        DB::beginTransaction();
        //备货单明细
        $sad = StockingApplyDetail::query()->find($applyDetailId);
        $sad->pur_status = StockingApplyDetail::PURCHASE_STATUS_CLOSE; //已关闭
        $flag1 = $sad->save();
        if(!$flag1){
            DB::rollBack();
            return false;
        }

        //备货单
        $detailPurStatus = StockingApplyDetail::query()->where('stocking_no',$sad->stocking_no)->pluck('pur_status')->toArray();
        $purStatus = StockingApplyDetail::extractStatus($detailPurStatus);
        $sa = StockingApply::query()->where('stocking_no',$sad->stocking_no)->first();
        $sa->pur_status = $purStatus;
        $flag2 = $sa->save();
        if(!$flag2){
            DB::rollBack();
            return false;
        }
        DB::commit();
        return true;
    }

    /**
     * 批量更新包材单价
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function packPrice($id)
    {
        return view('purchase.order-edit.pack-price',compact('id'));
    }

    /**
     * 批量更新包材单价
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function doPackPrice($param){
        try{
            DB::beginTransaction();
            $model = PurchaseOrder::query()->findOrFail($param['id']);
            if(!$model || $model->status != PurchaseOrder::STATUS_NO){ //未下单
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '状态有误！']);
            }
            $flag1 = false;
            foreach ($model->order_detail as $detail){
                //更新包材信息
                $detail->pack_price = $param['pack_price'];
                if($param['pack_price_desc']){
                    $detail->pack_price_desc = $param['pack_price_desc'];
                }
                $flag2 = $detail->save();
                if(!$flag2){
                    $flag1 = false;
                    break;
                }
                $flag1 = true;
            }
            if(!$flag1){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }else{
                DB::commit();
                return response()->json(['status' =>true, 'message' => '操作成功！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * 获取公共站点ID
     * @return int
     */
    private function _getSysAreaPublicId(){
        return SysArea::getSysAreaIdByArea();
    }
}
